/*******************************************************************************
 * Copyright (c) 2025-08-06 @author <a href="mailto:iffiff1@gmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@gmail.com">Tyler Chen</a> - initial API and implementation.
 ******************************************************************************/
package org.iff.util.llm.qwen;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * {
 *     "model": "qwen-vl-plus",
 *     "messages": [
 *         {
 *             "role": "user",
 *             "content": [
 *                 {
 *                     "type": "image_url",
 *                     "image_url": {
 *                         "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
 *                     }
 *                 },
 *                 {
 *                     "type": "text",
 *                     "text": "这是什么"
 *                 }
 *             ]
 *         }
 *     ]
 * }
 * </pre>
 *
 * @author <a href="mailto:iffiff1@gmail.com">Tyler Chen</a>
 * @since 2025-08-06
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ImageMessage {
    public static final String ROLE_USER = "user";
    public static final String ROLE_SYSTEM = "system";

    @JsonProperty("model")
    private String model;
    @JsonProperty("messages")
    private List<Messages> messages;

    public static ImageMessage of(){
        return new ImageMessage();
    }

    public ImageMessage model(String model) {
        this.model = model;
        return this;
    }

    public ImageMessage message(String role, String type, String imageUrl) {
        if(this.messages == null) {
            this.messages = new ArrayList<>();
        }
        Messages msg = Messages.builder().role(role).build();
        if(msg.content == null){
            msg.content = new ArrayList<>();
        }
        msg.content.add(Content.builder().type(type).imageUrl(ImageUrl.builder().url(imageUrl).build()).build());
        this.messages.add(msg);
        return this;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Messages {
        @JsonProperty("role")
        private String role;
        @JsonProperty("content")
        private List<Content> content;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Content {
        @JsonProperty("type")
        private String type;
        @JsonProperty("image_url")
        private ImageUrl imageUrl;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ImageUrl {
        @JsonProperty("url")
        private String url;
    }
}
